//
// Contains a function that fill an empty dungeon with ennemies
// and activators. Works with difficulty evaluators.
// Author : Alois Jobard
//

#include "global.h"

//RANDOM_RATE between 1 and 100 (100 = no random, 1 = max random)
#define RANDOM_RATE 20
//#define DEBUG

int filler(igraph_t graph, float average_diff, int start, igraph_vector_t end){
  int node;
  float missing_diff;
  float difficulty = average_diff * sizeevaluator1(graph);


  roominfo *ri;

  igraph_vit_t iterator;
  igraph_vs_t vs;
  igraph_vs_all(&vs);

  // iterator creation to explore all the nodes.
  igraph_vit_create(&graph, vs, &iterator);

  while (!IGRAPH_VIT_END(iterator)) {
    node = IGRAPH_VIT_GET(iterator);
         
    ri = get_roominfo(&graph, node);
    IGRAPH_VIT_NEXT(iterator);

    ri->enemy_diff = average_diff/2;
    // todo : remplissage des pointless areas avec des activateurs
    
  }
  /*  
  igraph_vector_ptr_t res;
  igraph_vector_ptr_init(&res, 2);
  igraph_vs_t to;
  igraph_vs_vector(&to, &end);

  for (i=0; i< igraph_vector_size(&end); i++) {
    igraph_vector_ptr_set(&res, i, &path);
  }
  igraph_get_shortest_paths(&graph, &res, start, to, IGRAPH_ALL);
  */

  missing_diff = (difficulty-evaldiff3(graph,start,end))/sizeevaluator1(graph);
  
  IGRAPH_VIT_RESET(iterator);
  
  while(missing_diff > 0.1){
#ifdef DEBUG
    printf("iter :%d\n miss : %f\n", i,missing_diff);i++;
#endif    
    while(!IGRAPH_VIT_END(iterator)){
      node = IGRAPH_VIT_GET(iterator);
      
      ri = get_roominfo(&graph, node);

      if(rand()%100 < RANDOM_RATE){
        get_roominfo(&graph, node);
        ri->enemy_diff += (missing_diff<1?1:2*missing_diff);
      }
      
      if(ri->activator_diff!=0){
        while(rand()%100 < 15){
          ri->activator_diff++;
        }
      }
      set_roominfo(&graph, node, ri);
      IGRAPH_VIT_NEXT(iterator);
    }
    missing_diff = (difficulty - evaldiff3(graph,start, end))/sizeevaluator1(graph);
    IGRAPH_VIT_RESET(iterator);
  }


#ifdef DEBUG
  //Affiche la difficulté de chaque piece dans le terminal 
  while(!IGRAPH_VIT_END(iterator)){
    node = IGRAPH_VIT_GET(iterator);
    ri = get_roominfo(&graph, node);
    printf("room %d difficulty: %d\n",node, ri->enemy_diff);

	dungeonmap_place_enemies(ri);

    IGRAPH_VIT_NEXT(iterator);
  }
  igraph_vit_destroy(&iterator);
#endif
  

  return 0;
}
